import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sleep/db/merroy_db.dart';
import 'package:sleep/db/sql_manger.dart';
import 'package:sleep/model/blue/blue_model.dart';
import 'package:sleep/model/person/person.dart';
import 'package:sleep/model/setting/setting.dart';

const kSleepPerson = "kSleepPerson";
const kFatiguePerson = "kFatiguePerson";
const kSleepBlue = 'kSleepBlue';
const kFaigueBlue = 'kFaigueBlue';
const kPlueBlue = 'kPlueBlue';

const kSleepSetting = 'kSleepSetting';
const kFaigueSetting = 'kFaigueSetting';
const kPlueSetting = 'kPlueSetting';

class DBNotifier extends ChangeNotifier {
  savePerson(Person person, String key) {
    if (key == kSleepPerson) {
      MerroyDB.share().l = person.l;
      MerroyDB.share().height = person.height;
      MerroyDB.share().sleepValue = person.value;
      MerroyDB.share().sleepMutile = person.multipe;
    }
    SQLManger.share().save(key: key, value: jsonEncode(person.toJson()));
    notifyListeners();
  }

  Person? getPerson(String key) {
    final person = SQLManger.share().get(key: key);
    if (person != null && person.isNotEmpty) {
      final personModel = Person.fromJson(jsonDecode(person));
      if (key == kSleepPerson) {
        MerroyDB.share().l = personModel.l;
        MerroyDB.share().height = personModel.height;
        MerroyDB.share().sleepValue = personModel.value;
        MerroyDB.share().sleepMutile = personModel.multipe;
      }
      return personModel;
    }
    return null;
  }

  saveBlueModel(BlueModel blue, String key) {
    SQLManger.share().save(key: key, value: jsonEncode(blue.toJson()));
    notifyListeners();
  }

  remove(String key) {
    SQLManger.share().delete(key);
    notifyListeners();
  }

  BlueModel? getBlueModel(String key) {
    String? blueStr = SQLManger.share().get(key: key);
    if (blueStr != null && blueStr.isNotEmpty) {
      return BlueModel.fromJson(jsonDecode(blueStr));
    }
    return null;
  }

  saveSetting({
    SleepSetting? sleep,
    PluseSetting? pluse,
    FatigueSetting? fatuige,
    bool? needNotify = false,
  }) {
    if (sleep != null) {
      SQLManger.share()
          .save(key: kSleepSetting, value: jsonEncode(sleep.toJson()));
    }
    if (pluse != null) {
      SQLManger.share()
          .save(key: kPlueSetting, value: jsonEncode(pluse.toJson()));
    }
    if (fatuige != null) {
      SQLManger.share()
          .save(key: kFaigueSetting, value: jsonEncode(fatuige.toJson()));
    }
    if (needNotify == true) notifyListeners();
  }

  SleepSetting sleepSetting() {
    String? setting = SQLManger.share().get(key: kSleepSetting);

    Map map = jsonDecode(setting ?? "{}");

    return SleepSetting.fromJson(
        map.combine(SleepSetting.defaultSetting().toJson()));
  }

  PluseSetting pluseSetting() {
    String? setting = SQLManger.share().get(key: kPlueSetting);
    Map map = jsonDecode(setting ?? "{}");

    return PluseSetting.fromJson(
        map.combine(PluseSetting.defaultSetting().toJson()));
  }

  FatigueSetting fatuigeSetting() {
    String? setting = SQLManger.share().get(key: kFaigueSetting);
    Map map = jsonDecode(setting ?? "{}");
    final defalutMap = FatigueSetting.defaultSetting().toJson();

    return FatigueSetting.fromJson(map.combine(defalutMap));
  }
}

final dbProvider = ChangeNotifierProvider<DBNotifier>((ref) {
  return DBNotifier();
});
